      
//连接数据库quest
const mongoose = require('mongoose')
mongoose.connect('mongodb://121.36.47.113:27017/quest')

//获取连接状态
const conn  = mongoose.connection
conn.on('connected',function(){
    console.log('数据库连成功！')
})

//infos表定义
const infoSchame = mongoose.Schema({
    key:{type:String,require:true},
    value:{type:Number,require:true}
})

//users表定义
const userSchame = mongoose.Schema({
    username:{type:String,require:true},
    password:{type:String,require:true}
})

//问卷表定义
const questSchame = mongoose.Schema({
    username:{type:String,require:true},
    questid:{type:Number,require:true},
    title:{type:String},
    description:{type:String},
    status:{type:String},
    question:{type:Array},
    creationtime:{type:Date,require:true},
    releasetime:{type:Date},
    deadline:{type:Date}
})

//答案表定义
const answerSchame = mongoose.Schema({
    questid:{type:Number,require:true},
    answer:{type:Array,require:true}
})

const userModel = mongoose.model('user',userSchame)

//插入数据
function insertUsers(){
    const auser = new userModel({username:'testuser1',password:'user1'})
    auser.save(function(error,data){
        console.log('Error:',error)
    })
}

//insertUsers()

//查找数据
function selectUsers(){
    //find findone两个可用方法
    userModel.find({}, function (error, data) {
        console.log('Error:',error,'Data:',data)
    })
}

//selectUsers()

//更新数据
function updateUsers(){
    //updateOne,updatemany两个可用方法
    userModel.updateOne({username:'testuser1'},{$set:{password:'user1_1'}},function (error, data) {
        console.log('Error:',error)
    })
}

//updateUsers()

//删除数据
function deleteUsers(){
    //deleteone deletemany两个可用方法
    userModel.deleteOne({username:'testuser1'},function (error, data) {
        console.log('Error:',error)
    })
}

deleteUsers()